package com.meatball.iot.common

import com.meatball.iot.common.mqtt.IMqttComponent
import com.meatball.iot.driver.doormagnet.DoorMagnetService
import com.meatball.iot.driver.dtu.DtuServerService
import com.meatball.iot.driver.envmonitoring.EnvMonitoringService
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component

@Component
class ApplicationRunnerImpl(
    private val dtuServerService: DtuServerService,
    private val doorMagnetService: DoorMagnetService,
    private val envMonitoringService: EnvMonitoringService,
    private val iMqttComponent: IMqttComponent
) : ApplicationRunner {
    /**
     * 创建一个固定大小的线程池，用于处理客户端连接
     */
    private val executorService = ClientConnectionThreadPool.executorService()

    override fun run(args: ApplicationArguments?) {
        executorService.execute {
            dtuServerService.startServer()
        }
        executorService.execute {
            doorMagnetService.startServer()
        }
        executorService.execute {
            envMonitoringService.startServer()
        }
        executorService.execute {
            iMqttComponent.startMqtt()
        }
    }
}